clear;
nColors = 3;

testimg = imread('flowers.jpeg');
figure(1), imshow(testimg);
cform = makecform('srgb2lab');
lab_testimg = applycform(testimg, cform);


ab_testimg = double(lab_testimg(:,:,2:3));
[nrows ncols nextra] = size(ab_testimg);
ab_testimg = reshape(ab_testimg, nrows*ncols, 2);
[cluster_idx  cluster_center] = kmeans(ab_testimg, nColors);
%show the clustered image on L value
pixel_labels = reshape(cluster_idx, nrows, ncols);
figure(2);
imshow(pixel_labels, []), title('image labeled based on color');

l_testimg = double(lab_testimg(:,:,1));
[nrows ncols] = size(l_testimg);
l_testimg = reshape(l_testimg, nrows*ncols, 1);
[cluster_idx  cluster_center] = kmeans(l_testimg, nColors);
%show the clustered image on L value
pixel_labels = reshape(cluster_idx, nrows, ncols);
figure(3);
imshow(pixel_labels, []), title('image labeled based on l value');
